package com.inossem.warehousestorage_inossem.activity.out_storage;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.allocation.out_storage.extra.AllocationOutStorageExtra;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.extra.OutStorageExtra;
import com.inossem.warehousestorage_inossem.adapter.outStorage.OutStorageDetailSNListAdapter;
import com.inossem.warehousestorage_inossem.bean.outStorage.ItemListBean;
import com.inossem.warehousestorage_inossem.bean.outStorage.SNBean;
import com.inossem.warehousestorage_inossem.bean.outStorage.SNItemInfo;
import com.inossem.warehousestorage_inossem.constant.TypeEnum;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.listener.OutStorageDetailSNListener;
import com.inossem.warehousestorage_inossem.request.outStorage.AddSNRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.OutStorageUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

//按序列号出库
public class OutStorageDetailSNActivity extends BaseActivity {

    private TextView title;

    private PullToRefreshLayout refresh;

    private PullableListView outStorageDetailList;

    private OutStorageDetailSNListAdapter adapter;

    private String mRid, mType, mCondition;

    private ItemListBean mMaterielDetail;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_out_storage_picking_detail);
        initView(OutStorageDetailSNActivity.this);
    }

    @Override
    protected void onResumeImpl() {
        initData(OutStorageDetailSNActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        outStorageDetailList = (PullableListView) activity.findViewById(R.id.pickingOutStorageDetailList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.pickingOutStorageDetailRefresh);
    }

    private void initData(Activity activity) {
        title.setText(getResources().getString(R.string.activity_out_storage_picking_detail_acquisition_of_subsidiary));
        ActivityUtils.initBackAndFunction(activity, getResources().getString(R.string.activity_out_storage_picking_add_operator_an_overview_of_the_acquisition), new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                updateInfo(OutStorageDetailSNActivity.this);
                OutStorageDetailSNActivity.this.finish();
            }
        });

        outStorageDetailList.setPullUpFlag(false);
        outStorageDetailList.setPullDownFlag(false);
        mCondition = "";
        getExtra(activity);
        getData(activity);
        setmScanListener((String scanResult, int length) -> {
            String type = QRCodeUtils.getQRCodeType(scanResult);
            switch (type) {
                case QRCodeConstant.CY_TYPE_PACKAGE:
                case QRCodeConstant.CY_TYPE_SN:
                    mCondition = scanResult;
                    getSN(activity, mCondition);
                    break;
            }
        }, QRCodeConstant.CY_TYPE_PACKAGE, QRCodeConstant.CY_TYPE_SN);
    }

    private void getExtra(Activity activity) {
        mRid = activity.getIntent().getStringExtra(OutStorageExtra.SERIAL);
        mType = activity.getIntent().getStringExtra(OutStorageExtra.TYPE);
    }

    //填充数据
    private void addOutStorageSNItemListData(Context context, ItemListBean materielDetail, List<SNBean.SNInfoBean> snList, String sn, ListView listView) {
        adapter = new OutStorageDetailSNListAdapter(context, materielDetail, snList, sn, mType, new OutStorageDetailSNListener() {

            @Override
            public void onGetSN(String sn) {
                mCondition = sn;
                getSN(context, sn);
            }

            @Override
            public void onDeleteItem(String code, String type) {
                switch (type) {
                    case OutStorageUtils.SNTYPE_PACKAGE:
                        deletePackageItem(context, code);
                        break;
                    case OutStorageUtils.SNTYPE_SN:
                        deleteSNItem(context, code);
                        break;
                }
            }

            @Override
            public void onLookPackageDetail(String packageCode) {
                updateInfo(context);
                openPackageDetail(context, packageCode);
            }
        });
        listView.setAdapter(adapter);
    }

    //打开打包码详情页面
    private void openPackageDetail(Context context, String packageCode) {
        List<SNItemInfo> snList = OutStorageUtils.getPackageDetail(mMaterielDetail.getSnList(), packageCode);
        Intent intent = new Intent(context, PackageDetailActivity.class);
        intent.putExtra(OutStorageExtra.SN_LIST, (Serializable) snList);
        startActivity(intent);
    }

    //删除序列号
    private void deleteSNItem(Context context, String code) {
        List<SNBean.SNInfoBean> snList = mMaterielDetail.getSnList();
        if (snList != null && !snList.isEmpty()) {
            for (int i = 0; i < snList.size(); i++) {
                SNBean.SNInfoBean snItem = snList.get(i);
                if (snItem.getType().equals(OutStorageUtils.SNTYPE_SN) && snItem.getSn_list().get(0).getSn_code().equals(code)) {
                    snList.remove(snItem);
                    break;
                }
            }
            mMaterielDetail.setSnList(snList);
        }
        addOutStorageSNItemListData(context, mMaterielDetail, mMaterielDetail.getSnList(), mCondition, outStorageDetailList);
    }

    //删除打包码
    private void deletePackageItem(Context context, String code) {
        List<SNBean.SNInfoBean> snList = mMaterielDetail.getSnList();
        if (snList != null && !snList.isEmpty()) {
            for (int i = 0; i < snList.size(); i++) {
                SNBean.SNInfoBean snItem = snList.get(i);
                if (snItem.getType().equals(OutStorageUtils.SNTYPE_PACKAGE) && snItem.getPackage_code().equals(code)) {
                    snList.remove(snItem);
                    break;
                }
            }
            mMaterielDetail.setSnList(snList);
        }
        addOutStorageSNItemListData(context, mMaterielDetail, mMaterielDetail.getSnList(), mCondition, outStorageDetailList);
    }

    //获取序列号或打包码
    private void getSN(Context context, String sn) {
        TypeEnum typeEnum = null;
        switch (mType) {
            case OutStorageUtils.OUT_STORAGE_TYPE_PICKING:
                //领料出库
                typeEnum = TypeEnum.OUT_STORAGE_PICKING;
                break;
            case OutStorageUtils.OUT_STORAGE_TYPE_SALE:
                //销售出库
                typeEnum = TypeEnum.OUT_STORAGE_SALE;
                break;
            case OutStorageUtils.OUT_STORAGE_TYPE_PRODUCTION:
                //生产领用
                typeEnum = TypeEnum.OUT_STORAGE_PRODUCTION;
                break;
            case OutStorageUtils.OUT_STORAGE_TYPE_ALLOCATION:
                //调拨出库
                typeEnum = TypeEnum.OUT_STORAGE_ALLOCATING;
                break;
        }
        AddSNRequest.request(context, mCondition, typeEnum, new GsonBeanListener<SNBean>() {
            @Override
            public void onSuccess(SNBean bean) {
                boolean result = OutStorageUtils.filterSN(mMaterielDetail.getSnList(), bean.getBody());
                if (!result) {
                    List<SNBean.SNInfoBean> resultSNList = mMaterielDetail.getSnList();
                    if (resultSNList == null) {
                        resultSNList = new ArrayList<>();
                    }
                    resultSNList.add(bean.getBody());
                    mMaterielDetail.setSnList(resultSNList);
                    addOutStorageSNItemListData(context, mMaterielDetail, mMaterielDetail.getSnList(), mCondition, outStorageDetailList);
                } else {
                    Utils.middleToast(context, "打包码或序列号已存在");
                }
            }
        });
    }

    //获取默认批次
    private void getData(Context context) {
        switch (mType) {
            case OutStorageUtils.OUT_STORAGE_TYPE_PICKING:
                //领料出库
                mMaterielDetail = OutStorageUtils.getPickingOutStorageDetailBySerial(context, mRid);
                addOutStorageSNItemListData(context, mMaterielDetail, mMaterielDetail.getSnList(), mCondition, outStorageDetailList);
                break;
            case OutStorageUtils.OUT_STORAGE_TYPE_SALE:
                //销售出库
                mMaterielDetail = OutStorageUtils.getSaleOutStorageDetailBySerial(context, mRid);
                addOutStorageSNItemListData(context, mMaterielDetail, mMaterielDetail.getSnList(), mCondition, outStorageDetailList);
                break;
            case OutStorageUtils.OUT_STORAGE_TYPE_PRODUCTION:
                //生产领用
                break;
            case OutStorageUtils.OUT_STORAGE_TYPE_ALLOCATION:
                //调拨出库
                int type = getIntent().getIntExtra(AllocationOutStorageExtra.TYPE, 0);
                switch (type) {
                    case 0:
                        mMaterielDetail = OutStorageUtils.getAllocationOutStorageDetailBySerial(context, mRid);
                        break;
                    case 1:
                        mMaterielDetail = OutStorageUtils.getAllocationOutStorageDetailByMatid(context, mRid);
                        break;
                }
                addOutStorageSNItemListData(context, mMaterielDetail, mMaterielDetail.getSnList(), mCondition, outStorageDetailList);
                break;
        }
    }

    @Override
    public void onBackPressed() {
        updateInfo(OutStorageDetailSNActivity.this);
        OutStorageDetailSNActivity.super.onBackPressed();
    }

    private void updateInfo(Context context) {
        switch (mType) {
            case OutStorageUtils.OUT_STORAGE_TYPE_PICKING:
                //领料出库
                mMaterielDetail.setActualSendNumber(adapter.getmActualSendNumber());
                OutStorageUtils.updatePickingOutStorageDetail(context, mMaterielDetail);
                break;
            case OutStorageUtils.OUT_STORAGE_TYPE_SALE:
                //销售出库
                mMaterielDetail.setActualSendNumber(adapter.getmActualSendNumber());
                OutStorageUtils.updateSaleOutStorageDetail(context, mMaterielDetail);
                break;
            case OutStorageUtils.OUT_STORAGE_TYPE_PRODUCTION:
                //生产领用
                break;
            case OutStorageUtils.OUT_STORAGE_TYPE_ALLOCATION:
                //调拨出库
                mMaterielDetail.setActualSendNumber(adapter.getmActualSendNumber());
                OutStorageUtils.updateAllocatingOutStorageDetail(context, mMaterielDetail);
                break;
        }
    }

}
